package com.hy.mydesktop.client.component.factory;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BaseListLoadResult;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.DataReader;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hy.mydesktop.client.component.meta.AbstractComponetModelEnum;
import com.hy.mydesktop.client.component.meta.ComboBoxModelEnum;
import com.hy.mydesktop.client.component.meta.TextFieldModelEnum;
import com.hy.mydesktop.client.rpc.ServiceInvoker;
import com.hy.mydesktop.client.rpc.ServiceInvokerAsync;
import com.hy.mydesktop.client.rpc.reader.GxtToSeamServiceResultListReader;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceParameter;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceResult;

/**
 * 
 * <ul>
 * <li>开发作者：汤莉</li>
 * <li>设计日期：2010-8-30；时间：上午09:39:09</li>
 * <li>类型名称：ComboBoxFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class SimpleComboBoxFactory {

	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建下拉列表框控件</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-30；时间：下午上午09:40:10</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaModel
	 * @return
	 */
	public static SimpleComboBox createSimpleComboBox(GxtComponentMetaNodeModel gxtComponentMetaModel){
		return configureSimpleComboBox(gxtComponentMetaModel);
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：配置下拉列表框的属性</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-30；时间：下午上午09:41:09</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static SimpleComboBox configureSimpleComboBox(GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		final SimpleComboBox box=createDefaultcomboBox();
		AbstractComponetFactory.configureAbstractComponet(box, gxtComponentMetaNodeModel);

		//数据加载
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODEL.name().toLowerCase())!=null){
			final ServiceInvokerAsync serviceInvoker = GWT.create(ServiceInvoker.class);
			final GxtToSeamServiceParameter gxtToSeamServiceParameter = box.getData("gxtToSeamServiceParameter");
//			final GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			//gxtToSeamServiceParameter.setServiceComponentName("com.test.server.business.PersonService");
			//gxtToSeamServiceParameter.setServiceMethodName("loadComboBoxData");
//			gxtToSeamServiceParameter.setServiceMethodName("getList2");
//			gxtToSeamServiceParameter.setServiceMethodName("getEnums");
			//BaseModel[] methodArguments2 = new BaseModel[1];
			//methodArguments2[0] = new BaseModel();
			//gxtToSeamServiceParameter.setMethodArguments(methodArguments2 );
			serviceInvoker.callService(gxtToSeamServiceParameter, new AsyncCallback<GxtToSeamServiceResult>() {

				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(GxtToSeamServiceResult result) {
					ModelData modelData = result.getResult();
					Object listOfString = modelData.get("listOfString");
					if (listOfString!=null&&listOfString instanceof List ) {
						for (String str : (List<String>)listOfString) {
							box.add(str);
						}
					}else {
						Object listOfModelData = modelData.get("listOfModelData");
						if (listOfModelData!=null&&listOfModelData instanceof List ) {
							Object testType = ((List)listOfModelData).get(0);
							if (testType instanceof String) {
								for (String str : (List<String>)listOfModelData) {
									box.add(str);
								}
							}else if (testType instanceof ModelData) {
								for (ModelData subModelData : (List<ModelData>)listOfModelData) {
									//box.add(subModelData.get(property));
								}
							}
							
						}
					}
					
				}
			});
			//box.setStore(listStore);
			//box.setDisplayField("name");
			
			//listStore.getLoader().load();
			
		}
		
		//数据加载
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.DATALOAD.name().toLowerCase())!=null){
			final ServiceInvokerAsync serviceInvoker = GWT.create(ServiceInvoker.class);
			final GxtToSeamServiceParameter gxtToSeamServiceParameter = box.getData("gxtToSeamServiceParameter");
//			final GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			//gxtToSeamServiceParameter.setServiceComponentName("com.test.server.business.PersonService");
			//gxtToSeamServiceParameter.setServiceMethodName("loadComboBoxData");
//			gxtToSeamServiceParameter.setServiceMethodName("getList2");
//			gxtToSeamServiceParameter.setServiceMethodName("getEnums");
			//BaseModel[] methodArguments2 = new BaseModel[1];
			//methodArguments2[0] = new BaseModel();
			//gxtToSeamServiceParameter.setMethodArguments(methodArguments2 );
			serviceInvoker.callService(gxtToSeamServiceParameter, new AsyncCallback<GxtToSeamServiceResult>() {

				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(GxtToSeamServiceResult result) {
					ModelData modelData = result.getResult();
					Object listOfString = modelData.get("listOfString");
					if (listOfString!=null&&listOfString instanceof List ) {
						for (String str : (List<String>)listOfString) {
							box.add(str);
						}
					}else {
						Object listOfModelData = modelData.get("listOfModelData");
						if (listOfModelData!=null&&listOfModelData instanceof List ) {
							for (String str : (List<String>)listOfModelData) {
								box.add(str);
							}
						}
					}
					
				}
			});
			
		}
		
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.STORE.name().toLowerCase())!=null){
			//为下拉框设置数据集
			//box.setStore((ListStore<ModelData>)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.STORE.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.DISPLAYFIELD.name().toLowerCase())!=null){
			//设置显示的属性
			box.setDisplayField((String)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.DISPLAYFIELD.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.EMPTYTEXT.name().toLowerCase())!=null){
			//设置空值文本
			box.setEmptyText((String)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.EMPTYTEXT.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.FIELDLABEL.name().toLowerCase())!=null){
			//设置标签
			box.setFieldLabel((String)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.FIELDLABEL.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.TYPEAHEAD.name().toLowerCase())!=null){
			//如有匹配的值会自动补全，并在下拉列表中显示
			box.setTypeAhead((Boolean)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.TYPEAHEAD.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.TRIGGERACTION.name().toLowerCase())!=null){
			//当触发器领域被激活，执行这个行为
			box.setTriggerAction((TriggerAction)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.TRIGGERACTION.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.TEMPLATE.name().toLowerCase())!=null){
			//设置模版
			box.setTemplate((String)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.TEMPLATE.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.LOADINGTEXT.name().toLowerCase())!=null){
			//设置加载时显示的文本
			box.setLoadingText((String)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.LOADINGTEXT.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.WIDTH.name().toLowerCase())!=null){
			//设置宽度
			box.setWidth((Integer)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.WIDTH.name().toLowerCase()));
			
		}
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.MAXHEIGHT.name().toLowerCase())!=null){
			//设置宽度
			box.setMaxHeight((Integer)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.MAXHEIGHT.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.NAME.name().toLowerCase())!=null){
			//设置控件名
			box.setName((String)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.NAME.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(ComboBoxModelEnum.ALLOWBLANK.name().toLowerCase())!=null){
			//设置控件是否允许为空
			box.setAllowBlank((Boolean)gxtComponentMetaNodeModel.get(ComboBoxModelEnum.ALLOWBLANK.name().toLowerCase()));
		}
		
		return box;
		
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建默认的下拉框</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-30；时间：下午上午09:42:27</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @return
	 */
	private static SimpleComboBox createDefaultcomboBox(){
		SimpleComboBox box=new SimpleComboBox();
		box.setFieldLabel("simpleCombobox");
		//ListStore<ModelData> store=new ListStore<ModelData>();
		//store.add(createStoreData());
		//box.setStore(store);
		//box.setDisplayField("name");
		return box;
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：测试数据</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-31；时间：下午下午03:15:22</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @return
	 */
	private static List<ModelData> createStoreData()
	{
		List<ModelData> list=new ArrayList<ModelData>();
		ModelData data=new BaseModelData();
		data.set("name", "唱歌");
		list.add(data);
		
		data=new BaseModelData();
		data.set("name", "跳舞");
		list.add(data);
		
		data=new BaseModelData();
		data.set("name", "画画");
		list.add(data);
		
		return list;
	}
}
